﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using gt.dotnetcore.webapi.DataAccess;
using gt.dotnetcore.webapi.extension.Filters;
using gt.dotnetcore.webapi.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using gt.dotnetcore.webapi.extension;

namespace gt.dotnetcore.webapi.Controllers
{
    //[ServiceFilter(typeof(DefaultExceptionFilterAttribute))]
    [Route("api/[controller]")]
    [Authorize("Permission")]
    //[BasicAuthorizationFilter]
    //[Authorize()]
    [ApiController]
    public class BookController : ApiControllerBase
    {
        private IBookDAL _bookDAL;

        public BookController(IBookDAL bookDAL)
        {
            _bookDAL = bookDAL;
        }

        /// <summary>
        /// 获取所有书籍
        /// </summary>
        /// <returns></returns>
        [Route("all")]
        [HttpGet]
        public IActionResult All()
        {
            var result = _bookDAL.GetAllBooks();
            return Success(result);
        }

        /// <summary>
        /// 获取所有书籍
        /// </summary>
        /// <returns></returns>
        [Route("get")]
        [HttpGet]
        public IActionResult Get(string name)
        {
            var result = _bookDAL.GetBook(name);
            return Success(result);
        }

        /// <summary>
        /// 新增书籍
        /// </summary>
        /// <param name="model">书籍模型</param>
        /// <returns></returns>
        [Route("add")]
        [HttpPost]
        public IActionResult Add(BookModel model)
        {
            if (model == null
                || string.IsNullOrEmpty(model.Name)
                || string.IsNullOrEmpty(model.Author))
            {
                return Error(-1, "参数错误");
            }
            _bookDAL.AddBook(model);
            return Success();
        }
    }
}